package com.itheima.autocreate;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@Log4j2
public class MyMetaObjecthandler implements MetaObjectHandler{              //自动生成时间和创建人的工具


        @Override
        public void insertFill(MetaObject metaObject) {
            long id = Thread.currentThread().getId();
            log.info("线程id为：{}",id);


            log.info("公共字段自动填充[insert]...");
            log.info(metaObject.toString());
            if (metaObject.hasGetter("createTime")) {
                metaObject.setValue("createTime", new Date().getTime());
            }
            if (metaObject.hasGetter("createUser")) {
                metaObject.setValue("createUser", Thread.currentThread().getId());

            }
            if (metaObject.hasGetter("orderTime")) {
                metaObject.setValue("orderTime",new Date().getTime());

            }
            if (metaObject.hasGetter("checkoutTime")) {
                metaObject.setValue("checkoutTime",new Date().getTime());
            }

        }
        /**
         * 更新操作 自动填充
         * @param metaObject
         */
        @Override
        public void updateFill(MetaObject metaObject) {
            log.info("公共字段自动填充[update]...");
            log.info(metaObject.toString());
            if (metaObject.hasGetter("updateTime")) {
                metaObject.setValue("updateTime", new Date().getTime());
            }
            if (metaObject.hasGetter("updateUser")) {
                metaObject.setValue("updateUser", Thread.currentThread().getId());
            }
        }
    }


